package funkcje;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class OperacjeNaPlikach {
	
	public void zapisz(ArrayList<Double> wartosci, File plik){
		
		try{
			DataOutputStream zapisz = new DataOutputStream(new FileOutputStream(plik));
			for(int i =0; i<wartosci.size(); i++){
				zapisz.writeDouble(wartosci.get(i));
			}
			zapisz.close();
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	
	public ArrayList<Double> otworz(File plik){
		ArrayList<Double> wartosci = new ArrayList<Double>();
		
		try{
			boolean koniec = false;
			DataInputStream otworz = new DataInputStream(new FileInputStream(plik));
			
			while(!koniec){
				try{
				double temp = otworz.readDouble();
				wartosci.add(temp);
				}
				catch(IOException ex){
					koniec=true;
				}
			}
		}
		catch(FileNotFoundException exception){
			JOptionPane.showMessageDialog(null,"Nie znaleziono pliku");
		}
				
		return wartosci;
		
	}
	
	
}
